Need Help in optimizing a loop in C [migrated]

Posted by WedaPashi on Programmers See other posts from Programmers or by WedaPashi
Published on 2013-06-28T10:54:49Z Indexed on 2013/06/28 16:30 UTC
Read the original article Hit count: 370

Filed under:

I am trying to draw a Checkerboard pattern on a lcd using a GUI library called emWin. I have actually managed to draw it using the following code. But having these many loops in the program body for a single task, that too in the internal flash of the Microcontroller is not a good idea. Those who have not worked with emWin, I will try and explain a few things before we go for actual logic. GUI_REST is a structure which id define source files of emWin and I am blind to it. Rect, REct2,Rec3.. and so on till Rect10 are objects. Elements of the Rect array are {x0,y0,x1,y1}, where x0,y0 are starting locations of rectangle in X-Y plane and x1, y1 are end locations of Rectangle in x-Y plane.

So, Rect={0,0,79,79} is a rectangle starts at top left of the LCD and is upto (79,79), so its a square basically. The function GUI_setBkColor(int color); sets the color of the background. The function GUI_setColor(int color); sets the color of the foreground. GUI_WHITE and DM_CHECKERBOARD_COLOR are two color values, #defineed GUI_FillRectEx(&Rect); will draw the Rectangle.

The code below works fine but I want to make it smarter.

GUI_RECT Rect = {0, 0, 79, 79};  
GUI_RECT Rect2 = {80, 0, 159, 79};  
GUI_RECT Rect3 = {160, 0, 239, 79};  
GUI_RECT Rect4 = {240, 0, 319, 79};  
GUI_RECT Rect5 = {320, 0, 399, 79};  
GUI_RECT Rect6 = {400, 0, 479, 79};  
GUI_RECT Rect7 = {480, 0, 559, 79};  
GUI_RECT Rect8 = {560, 0, 639, 79};    
GUI_RECT Rect9 = {640, 0, 719, 79};  
GUI_RECT Rect10 = {720, 0, 799, 79};  

    WM_SelectWindow(Win_DM_Main);
    GUI_SetBkColor(GUI_BLACK);
    GUI_Clear();

    for(i = 0; i < 6; i++)
    {
        if(i%2 == 0)
            GUI_SetColor(GUI_WHITE);
        else
            GUI_SetColor(DM_CHECKERBOARD_COLOR);

        GUI_FillRectEx(&Rect);

        Rect.y0 += 80;
        Rect.y1 += 80;
    }

    /*
    for(j=0,j<11;j++)
    {
        for(i = 0; i < 6; i++)
        {
            if(i%2 == 0)
                GUI_SetColor(GUI_WHITE);
            else
                GUI_SetColor(DM_CHECKERBOARD_COLOR);

                GUI_FillRectEx(&Rect);
                Rect.y0 += 80;
                Rect.y1 += 80;
        }
        Rect.x0 += 80;
        Rect.x1 += 80;
    }
    */



    for(i = 0; i < 6; i++)
    {
        if(i%2 == 0)
            GUI_SetColor(DM_CHECKERBOARD_COLOR);    
        else
            GUI_SetColor(GUI_WHITE);

        GUI_FillRectEx(&Rect2);

        Rect2.y0 += 80;
        Rect2.y1 += 80;
    }

    for(i = 0; i < 6; i++)
    {
        if(i%2 == 0)
            GUI_SetColor(GUI_WHITE);
        else
            GUI_SetColor(DM_CHECKERBOARD_COLOR);

        GUI_FillRectEx(&Rect3);

        Rect3.y0 += 80;
        Rect3.y1 += 80;
    }

    for(i = 0; i < 6; i++)
    {
        if(i%2 == 0)
            GUI_SetColor(DM_CHECKERBOARD_COLOR);    
        else
            GUI_SetColor(GUI_WHITE);

        GUI_FillRectEx(&Rect4);

        Rect4.y0 += 80;
        Rect4.y1 += 80;
    }

    for(i = 0; i < 6; i++)
    {
        if(i%2 == 0)
            GUI_SetColor(GUI_WHITE);
        else
            GUI_SetColor(DM_CHECKERBOARD_COLOR);

        GUI_FillRectEx(&Rect5);

        Rect5.y0 += 80;
        Rect5.y1 += 80;
    }

    for(i = 0; i < 6; i++)
    {
        if(i%2 == 0)
            GUI_SetColor(DM_CHECKERBOARD_COLOR);    
        else
            GUI_SetColor(GUI_WHITE);

        GUI_FillRectEx(&Rect6);

        Rect6.y0 += 80;
        Rect6.y1 += 80;
    }

    for(i = 0; i < 6; i++)
    {
        if(i%2 == 0)
            GUI_SetColor(GUI_WHITE);
        else
            GUI_SetColor(DM_CHECKERBOARD_COLOR);

        GUI_FillRectEx(&Rect7);

        Rect7.y0 += 80;
        Rect7.y1 += 80;
    }

    for(i = 0; i < 6; i++)
    {
        if(i%2 == 0)
            GUI_SetColor(DM_CHECKERBOARD_COLOR);    
        else
            GUI_SetColor(GUI_WHITE);

        GUI_FillRectEx(&Rect8);

        Rect8.y0 += 80;
        Rect8.y1 += 80;
    }

    for(i = 0; i < 6; i++)
    {
        if(i%2 == 0)
            GUI_SetColor(GUI_WHITE);
        else
            GUI_SetColor(DM_CHECKERBOARD_COLOR);

        GUI_FillRectEx(&Rect9);

        Rect9.y0 += 80;
        Rect9.y1 += 80;
    }

    for(i = 0; i < 6; i++)
    {
        if(i%2 == 0)
            GUI_SetColor(DM_CHECKERBOARD_COLOR);    
        else
            GUI_SetColor(GUI_WHITE);

        GUI_FillRectEx(&Rect10);

        Rect10.y0 += 80;
        Rect10.y1 += 80;
    }

© Programmers or respective owner

Related posts about loops